/*
 * FSYS.H Version 1.01 (C)1995,1996 HUDSON SOFT
 * バックアップメモリ関連 Ｃコンパイラ用ヘッダーファイル(FSYS.H)
 */

#ifndef _FSYS_H_
#define _FSYS_H_

/* DEV_CTRL & HND_CTRL NO */
#define CTRL_VEC_RD	0
#define CTRL_VEC_WT	1
#define CTRL_NAME_RD	2
#define CTRL_NAME_WT	3
#define CTRL_OPT_JOB	4
#define	CTRL_IPLREAD	5
#define	CTRL_IPLWRITE	6
#define CTRL_BPBGET	7
#define CTRL_BPBSET	8
#define CTRL_GETSTAT	9

/* open mode */
#define	O_READ		0x0001
#define	O_WRITE		0x0002
#define	O_APPEND	0x0008	/* set append mode */
#define	O_CREAT		0x0200	/* create if nonexistant */
#define	O_TRUNC		0x0400	/* truncate to zero length */
#define	O_EXCL		0x0800	/* error if already exists */

/* error trap function */
#define IO_READ		0
#define IO_WRITE	1
#define IO_CHECK	2
#define	IO_CTRL		3
#define IO_WCHECK	4

/* seek base */
#define	SEEK_SET	0	/* TOF base */
#define	SEEK_CUR	1	/* current base */
#define	SEEK_END	2	/* EOF base */

/* atr type */
#define ATR_ARC		0x20
#define ATR_DIR		0x10
#define ATR_VOL		0x08
#define ATR_SYS		0x04
#define ATR_HID		0x02
#define ATR_RDO		0x01

/* ipl block data offset */
#define bt_jmp		0
#define bt_oem		3
#define bt_secsiz	11
#define bt_clsiz	13
#define bt_nrsvsect	14
#define bt_nfat		16
#define bt_dirents	17
#define bt_maxsect	19
#define bt_mid		21
#define bt_fatlen	22
#define bt_nsect	24
#define bt_nheads	26
#define bt_nhs		28
#define bt_bigsect	32
#define bt_junk		36

/* error code */
#define BAD_PARA   -1
#define BAD_DRIVE  -10
#define PATH_OVER  -11
#define NAME_OVER  -12
#define EXT_OVER   -13
#define BAD_DEVICE -14
#define NO_FILES   -15
#define BAD_PATH   -16
#define BAD_NAME   -17
#define MOUNTED    -18
#define UMOUNTED   -19
#define MOUNTBUSY  -20
#define FCB_EMPTY  -21
#define HND_EMPTY  -22
#define BAD_HND    -23
#define BAD_MODE   -24
#define SEEK_OVER  -25
#define FSYS_DIFF  -26
#define CUR_DIR    -27
#define BAD_CMD    -28
#define NOT_FMT    -29
#define NO_MEM     -30
#define IO_BUSY    -31
#define FILE_EXIST -32
#define BAD_FAT    -33
#define DIR_FULL   -34
#define DISK_FULL  -35
#define BAD_EXE    -36
#define KEEP_PR    -37
#define WRITE_PRO  -38
#define NOT_RDY    -39
#define SEC_NFND   -40
#define CRC_ERROR  -41
#define OVER_RUN   -42

typedef struct fsys {
  unsigned char name[16];
  int (* dev_call)();
  void *dev_type;
  long access_f;
  struct fsys *back;
  struct fsys *next;
  int path_len;
  unsigned char path[128];
} FSYS ;

typedef struct filesbf {
  int fsys_flg;
  FSYS *fsysp;
  unsigned char temp[64];
  long fsize;
  long ftime;
  int atr;
  unsigned char path[128];
  unsigned char *name;
  unsigned char *upath;
  int name_max;
} FILESBF ;

typedef struct dpb {
  int  dev_id;			/* SCSI_ID or hnd */
  long rec_offset;		/* record offset */
  int  rec_sft;			/* dos_record -> bios_rec */
  int  sft_byt;			/* bios_rec -> bytes */
  int (* dev_call)();		/* device i/o job */
  unsigned short ej_flg;	/* eject ok(=1) ng(=0) flg */
  unsigned short secsiz;	/* Bytes per sector */
  unsigned char secsft;		/* Bytes per sector sft count */
  unsigned char clsiz;		/* Sector per Cluster */
  unsigned char clsft;		/* Sector per Cluster  sft count */
  unsigned char mid;		/* Media descriptor */
  unsigned long fattop;		/* fat top sector */
  unsigned char nfat;		/* Number of FAT tables */
  unsigned char fatlen;		/* Sectors in FAT */
  unsigned short fat_max;	/* fat data max 2-??? */
  unsigned long dirtop;		/* root dir top sector */
  unsigned short dircnt;	/* root dir entrys per sector */
  unsigned short dirlen;	/* root dir sectors */
  unsigned long datatop;	/* data top sector */
  long free_fat;		/* free fat count (-1=no calc) */
  unsigned short new_fat;	/* last accsess new fat */
  unsigned short def_fat;	/* curdir fat(root=0) */
  unsigned char def_dir[64];	/* curdir dir name(root="") */
} DPB ;

typedef struct bpb {
  unsigned short secsiz;	/* Bytes per sector */
  unsigned char clsiz;		/* Cluster size */
  unsigned char nfat;		/* Number of FAT tables */
  unsigned short nrsvsect;	/* Number of reserved sectors */
  unsigned short dirents;	/* Number of root directory entry */
  unsigned short maxsect;	/* Total sectors on disk */
  unsigned char mid;		/* Media descriptor */
  unsigned char fatlen;		/* Sectors in FAT */
  unsigned long bigsect;	/* big total sectors (valid maxsect zero) */
  unsigned short nsect;		/* Sectors/track */
  unsigned short nheads;	/* Heads */
  unsigned long nhs;		/* number of hidden sectors */
} BPB ;

int	fsys_init(unsigned char *sptr,unsigned char *eptr,int (*err_trap)());
int	fsys_mount(unsigned char *path,FSYS *fsys);
int	fsys_ctrl(unsigned char *path,int no,unsigned char *buf,int len);
FSYS	*fsys_getfsys(unsigned char *path);
int	fsys_format(unsigned char *path,unsigned char *fmt_id);
long	fsys_diskfree(unsigned char *path);
long	fsys_getblocks(unsigned char *path);
int	fsys_open(unsigned char *path,int mode);
int	fsys_read(int hnd,unsigned char *bf,int length);
int	fsys_write(int hnd,unsigned char *bf,int length);
long	fsys_seek(int hnd,long offset,int mode);
long	fsys_htime(int hnd,long time);
int	fsys_close(int hnd);
int	fsys_delete(unsigned char *path);
int	fsys_rename(unsigned char *old_path,unsigned char *new_name);
int	fsys_mkdir(unsigned char *path);
int	fsys_rmdir(unsigned char *path);
int	fsys_chdir(unsigned char *path);
int	fsys_curdir(unsigned char *buff);
int	fsys_ffiles(FILESBF *bf,unsigned char *path);
int	fsys_nfiles(FILESBF *bf);
int	fsys_efiles(FILESBF *bf);
unsigned long	fsys_datetime(unsigned long time);
int	fsys_m_init(unsigned char *sptr,unsigned char *eptr);
void	*fsys_malloc(int len);
int	fsys_free(void *mp);
int	fsys_setblock(char *mp,int new_len);

#endif /* _FSYS_H_ */
